package com.trussell.genesis.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

import javax.swing.JOptionPane;

import com.trussell.genesis.Genesis;

/**
 * A class designed to read the configuration file that
 * will ship with the editor. It will contain info 
 * regarding the default height, width, workspace,
 * and other information critical to smooth operation.
 * 
 *
 * @author Tyler Trussell
 */
public class ConfigManager
{
	/*
	 * Make this a Singleton class.
	 */
	private static ConfigManager instance;
	
	/*
	 * For getting the instance.
	 */
	public static ConfigManager getInstance() {
		if(instance == null)
			instance = new ConfigManager();
		return instance;
	}
	
	private String fileName = "config/settings.cfg";
	private File configFile;
	
	/**
	 * Not for external use. @see ConfigManager.getInstance()
	 * TODO: make the error dialog actually show up.
	 * Right now, if the file is missing the system crashes horribly.
	 */
	private ConfigManager() {
		configFile = new File(fileName);
		if(!configFile.exists()) {
			JOptionPane.showMessageDialog(Genesis.getInstance(),
					"Error: Could not locate config file.\n" +
					"Error code: 1", "ERROR", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
	/**
	 * Returns the default height from the config file.
	 * TODO: handle exception more gracefully.
	 * TODO: fix bugs. always returns 512
	 */
	public int getDefaultHeight() {
		Scanner sn;
		try {
			sn = new Scanner(configFile);
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_height=")) {
					int toReturn = Integer.valueOf(line.substring("default_height=".length()));
					if(toReturn == 0)
						return 512;
					System.out.println(toReturn);
					return toReturn;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return 512;
	}
	
	/**
	 * Returns the default width from the config file.
	 * TODO: handle exception more gracefully.
	 * TODO: fix bugs. always returns 768
	 */
	public int getDefaultWidth() {
		Scanner sn;
		try {
			sn = new Scanner(configFile);
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_width=")) {
					int toReturn = Integer.valueOf(line.substring("default_width=".length()));
					if(toReturn == 0)
						return 768;
					return toReturn;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return 768;
	}
	
	public File getDefaultWorkspace() {
		Scanner sn;
		try {
			sn = new Scanner(configFile);
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_workspace=")) {
					return new File(line.substring("default_workspace=".length()));
				}
			}
			return null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Sets the new default height in the config file.
	 * @param newHeight - the new default height.
	 * 
	 * TODO: handle exceptions more gracefully.
	 */
	public void setDefaultHeight(int newHeight) {
		Scanner sn;
		try {
			sn = new Scanner(configFile);
			String fileContents = "";
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_height=")) {
					fileContents += "\ndefault_height="+newHeight;
				}
				else
					fileContents += "\n" + line;
			}
			configFile.delete();
			configFile.createNewFile();
			FileOutputStream fos = new FileOutputStream(configFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write(fileContents.trim());
			osw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Sets the new default height in the config file.
	 * @param newWidth - the new default width.
	 * 
	 * TODO: handle exceptions more gracefully.
	 */
	public void setDefaultWidth(int newWidth) {
		Scanner sn;
		try {
			sn = new Scanner(configFile);
			String fileContents = "";
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_width=")) {
					fileContents += "\ndefault_width="+newWidth;
				}
				else
					fileContents += "\n" + line;
			}
			configFile.delete();
			configFile.createNewFile();
			FileOutputStream fos = new FileOutputStream(configFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write(fileContents.trim());
			osw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setDefaultWorkspace(File f) {
		System.out.println("Setting workspace");
		Scanner sn;
		try {
			boolean wrote = false;
			sn = new Scanner(configFile);
			String fileContents = "";
			while(sn.hasNextLine()) {
				String line = sn.nextLine();
				if(line.startsWith("default_workspace=")) {
					fileContents += "\ndefault_workspace="+f.getPath();
					wrote = true;
				}
				else
					fileContents += "\n" + line;
			}
			if(!wrote) {
				fileContents += "\ndefault_workspace="+f.getPath();
				System.out.println("wrote");
			}
			configFile.delete();
			configFile.createNewFile();
			FileOutputStream fos = new FileOutputStream(configFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write(fileContents.trim());
			osw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
